package com.hgyc.mom.base.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.base.entity.ProductSeries;
import com.hgyc.mom.base.vo.ProductSeriesParam;
import com.hgyc.mom.base.service.ProductSeriesService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 产品系列Controller
 *
 * @author fwj
 * @since 2025-04-28
 */
@Tag(name = "产品系列控制器", description = "产品系列控制器")
@RestController
@RequestMapping("/base/series")
public class ProductSeriesController
{
    @Autowired
    private ProductSeriesService productSeriesService;

    @Operation(summary = "查询产品系列列表", description = "查询产品系列列表")
    @PostMapping("/pageList")
    public Result<IPage<ProductSeries>> pageList(@Parameter(description = "查询参数") @RequestBody ProductSeriesParam productSeriesParam)
    {
        Page<ProductSeries> page = new Page<>(productSeriesParam.getPage(), productSeriesParam.getPageSize());
        ProductSeries productSeries = new ProductSeries();
        BeanUtils.copyProperties(productSeriesParam, productSeries);
        QueryWrapper<ProductSeries> wrapper = QueryWrapperUtil.build(productSeries);
        IPage<ProductSeries> list = productSeriesService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "下拉系列列表", description = "下拉系列列表")
    @GetMapping("/selectList")
    public Result<List<ProductSeries>> selectList() {
        return Result.success(productSeriesService.list());
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ProductSeries> util = new ExcelUtil<>(ProductSeries.class);
        util.importTemplateExcel(response, "产品系列" );
    }

    @Operation(summary = "导出产品系列列表", description = "导出产品系列列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductSeriesParam productSeriesParam)
    {
        ProductSeries productSeries = new ProductSeries();
        BeanUtils.copyProperties(productSeriesParam, productSeries);
        QueryWrapper<ProductSeries> wrapper = QueryWrapperUtil.build(productSeries);
        List<ProductSeries> list = productSeriesService.list(wrapper);
        ExcelUtil<ProductSeries> util = new ExcelUtil<>(ProductSeries.class);
        util.exportExcel(response, list, "产品系列数据");
    }

    @Operation(summary = "导入产品系列列表", description = "导入产品系列列表")
    @PostMapping("/import")
    public Result importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ProductSeries> util = new ExcelUtil<>(ProductSeries.class);
        List<ProductSeries> productSeriesList = util.importExcel(file.getInputStream());
        String message = productSeriesService.importData(productSeriesList, updateSupport);
        return Result.success(message);
    }

    @Operation(summary = "获取产品系列详细信息", description = "获取产品系列详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "产品系列ID") @PathVariable("id") Long id)
    {
        return Result.success(productSeriesService.getById(id));
    }

    @Operation(summary = "新增产品系列", description = "新增产品系列")
    @PostMapping
    public Result save(@Parameter(description = "产品系列") @RequestBody ProductSeries productSeries)
    {
        return Result.success(productSeriesService.save(productSeries));
    }

    @Operation(summary = "修改产品系列", description = "修改产品系列")
    @PutMapping
    public Result update(@Parameter(description = "产品系列") @RequestBody ProductSeries productSeries)
    {
        return Result.success(productSeriesService.updateById(productSeries));
    }

    @Operation(summary = "删除产品系列", description = "删除产品系列")
    @DeleteMapping
    public Result remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids)
    {
        return Result.success(productSeriesService.removeByIds(Arrays.asList(ids)));
    }

}
